Skyline

Table of Contents

This is a “pair” assignment, which means that if you are working on a team with someone else, you and your partner should do your best to engage in the pair programming model. At any point in time, one of you is “driving,” i.e. actually using the keyboard and mouse. The other is actively engaged following along, preventing bugs, and providing ideas.

You should make sure that over the course of an assignment that you spend roughly the same amount of time each “driving.” I will also ask you to turn in a form rating the work that your partner does. My recommendation is to take turns approximately every 15 minutes or so. Set a timer to help you remember.

1. Overview

Many complicated movie scenes that you see involve lots of individually generated objects, all assembled together on one screen. Lots of movies use simulations where each fighter in a battle is a computer generated object. Similarly, computer generated movie scenes of a city (such as in many of the Marvel superhero movies, for example) involve superimposing a number of buildings of different shapes and sizes. For this assignment, you will create a city by drawing a number of buildings of varied sizes.

You will create a class named Building (in a file called building.py) to represent a building in a city. I will provide a file names skyline.py, which contains a main method. If you code your Building class correctly, you’ll get a city skyline!

2. Get started

Create a folder in which to store your work for this assignment.

  • If you are working on your own computer, it’s up to you where to put the folder. Your desktop is likely as good a place as any. Make a folder titled skyline.
  • If you are working in the labs in Olin, make sure to first mount the COURSES folder, so that you won’t lose your code when you log out. Once you’ve done so, open up Finder, then navigate to your personal student work folder. You can then make a skyline folder within there.
  • Once you’ve done so, you should then open up your new folder in VS Code. To do so, start up VS Code, then drag your folder onto the VS Code window. This should open up the folder within VS Code. If you are asked, click that you trust the authors.

Since we’ll be doing graphics, you’ll again need the graphics.py file that we used in the early graphics assignments.

3. Methods you will write

The Building class should have the following methods:

  • a constructor, which takes no parameters at all (except the usual `self`)
  • setHeightRandom(maxHeight)

    sets the height to a random value from 0 to maxHeight, inclusive

  • setWidthRandom(maxWidth)

    sets the height to a random value from 0 to maxWidth, inclusive

  • setLocationRandom(windowWidth)

    sets the horizontal location randomly, using the window width to know what the possible range of values are

  • setColorRandom()

    sets the building fill color randomly. The outline should always be black. You might find this section of the graphics.py documentation useful.

  • area()

    returns the area of the building (width * height). This is used for determining real-estate taxes.

  • draw(window, windowHeight)

    given a graphics window and its height, draw the building. The reason that you need the height of the window is to figure out where to place the building vertically. For purposes of this assignment, you can draw the building as a rectangle.

4. Some more details and advice

Test your code as you go along. Put your main function in a separate file called skylinetryit.py, and include the code

from building import *

at the top of it. As you write methods in your Building class, test them in main in skylinetryit.py. I strongly recommend that you seed your random number generator so that you get the same behavior every time you run your program. It’s much easier to debug that way.

When you are all done, test your code against the skyline.py that I provide. If you have built your Building class correctly, my skyline.py program should generate an awesome skyline for you to look at.

5. Exemplary

If you complete the above functionality, you should feel proud and good about yourself that you have gotten this far, and feel free to stop here! If you want to try to go for the E grade, make the building look more realistic. Specifically, add an antenna to the top of the building whose height is proportional to the rest of the building. Draw windows running down and across the buildings. The height of the windows should not be proportional to the height of the building; they should have a fixed width and height. This means that taller buildings and wider buildings will have more windows.

6. Grading

You will receive an M for this assignment if…

  • your skyline.py file is unmodified from the one I provided. (Remember, you can play around with your own called skylinetryit.py.)
  • when we run python skyline.py, it correctly generates a skyline of buildings, and returns a total area for those buildings.
  • your program is written to work in general, and not to only work for the specific skyline.py that I have provided.

You will receive a grade of E for this assignment if you satisfy the above M requirements, and …

  • your programs have a comment at the top of each method with at least 5 words describing what the function does.
  • every one of your variable names is meaningful in some way. (Names such as thing, number, etc. are not meaningful.)
  • You have at least one other comment near what you think is the trickiest part of your code, describing how it works
  • your code demonstrates a clear sequence of actions to achieve the goal at hand, and each piece is essential. Your code does not have notably more cases or conditions than it needs to.
  • you have successfully drawn antennas and windows as described above.

7. Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant